from django.db import models


class StoryTag(models.Model):
    """小说标签表"""
    tag = models.CharField(max_length=100, help_text='标签名称')
    create_time = models.DateTimeField(auto_now_add=True, help_text='创建时间')
    update_time = models.DateTimeField(auto_now=True, help_text='更新时间')

    class Meta:
        db_table = 'story_tag'
        ordering = (
            '-create_time',
            '-id'
        )

    def __str__(self):
        return f'<StoryTag:({self.pk}, {self.tag})>'

    __repr__ = __str__


class StoryComment(models.Model):
    comment_user = models.ForeignKey('frontuser.FrontUser', on_delete=models.CASCADE, related_name='comments',
                                     help_text='评论用户ID')
    content = models.TextField(null=True, blank=True, help_text='评论内容')
    reply_comment_user = models.ForeignKey('frontuser.FrontUser', on_delete=models.CASCADE, null=True, blank=True,
                                           related_name='reply_comments', help_text='回复评论用户ID')
    create_time = models.DateTimeField(auto_now_add=True, help_text='创建时间')
    update_time = models.DateTimeField(auto_now=True, help_text='更新时间')
    # 构建外键，story表删除数据级联删除
    story = models.ForeignKey('Story', on_delete=models.CASCADE, help_text='story主键ID')
    # 模型的外键引用的是本身自己这个模型，to参数可以为'self'
    comment = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, help_text='评论回复主键ID')

    class Meta:
        db_table = 'story_comments'
        ordering = (
            '-create_time',
            '-id'
        )

    def __str__(self):
        return f'<StoryComment:({self.pk}, {self.create_time})>'

    __repr__ = __str__


def default_tag():
    return StoryTag.objects.filter(tag__exact='默认分类').first()


class Story(models.Model):
    """小说表"""
    name = models.CharField(max_length=100, help_text='小说书名')
    pub_time = models.DateField(null=True, blank=True, help_text='发布时间')
    create_time = models.DateTimeField(auto_now_add=True, help_text='创建时间')
    update_time = models.DateTimeField(auto_now=True, help_text='更新时间')
    # 构建外键，front_user表删除数据级联删除
    front_user = models.ForeignKey('frontuser.FrontUser', on_delete=models.CASCADE, null=True, blank=True,
                                   help_text='发布用户ID')
    # 构建外键，story表删除数据级联删除
    # TODO models.CASCADE
    tag = models.ForeignKey('StoryTag', on_delete=models.CASCADE, null=True, blank=True, related_name='stories',
                            help_text='story_tag主键ID')
    # 构建多对多模型
    subjects = models.ManyToManyField('StorySubject', related_name='stories', help_text='专题')

    # TODO models.PROTECT
    # tag = models.ForeignKey('StoryTag', on_delete=models.PROTECT, null=True, blank=True,help_text='story_tag主键ID')
    # TODO models.SET_NULL
    # tag = models.ForeignKey('StoryTag', on_delete=models.SET_NULL, null=True, blank=True, help_text='story_tag主键ID')
    # TODO models.SET_DEFAULT
    # tag = models.ForeignKey('StoryTag', on_delete=models.SET_DEFAULT, default=default_tag, null=True, blank=True,
    #                         help_text='story_tag主键ID')
    # TODO models.SET(value)
    # tag = models.ForeignKey('StoryTag', on_delete=models.SET(default_tag), null=True, blank=True, help_text='story_tag主键ID')
    # TODO models.DO_NOTHING
    # tag = models.ForeignKey('StoryTag', on_delete=models.DO_NOTHING, null=True, blank=True, help_text='story_tag主键ID')

    class Meta:
        db_table = 'story'
        ordering = (
            '-pub_time',
            '-create_time',
            '-id'
        )

    def __str__(self):
        return f'<Story:({self.pk}, {self.name}, {self.pub_time})>'

    __repr__ = __str__


class StorySubject(models.Model):
    name = models.CharField(max_length=100, help_text='专题名称')
    create_time = models.DateTimeField(auto_now_add=True, help_text='创建时间')
    update_time = models.DateTimeField(auto_now=True, help_text='更新时间')

    class Meta:
        db_table = 'story_subject'
        ordering = (
            '-create_time',
            '-id'
        )

    def __str__(self):
        return f'<StorySubject:({self.pk}, {self.name})>'

    __repr__ = __str__
